home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / program / 316 / libsrc / fgetc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-20  |  1.0 KB  |  55 lines

  1.  
  2. /* fgetc */
  3.  
  4. #include <file.h>
  5. #include "std-guts.h"
  6.  
  7. int fgetc(f)
  8. struct file * f;
  9. {
  10.  
  11.   if (!f) return(EOF);
  12.   if (f->eof_p) return(EOF);
  13.   if (!f->open_p) return(EOF);
  14.   if ((f->mode & 0x03) == O_WRONLY) return(EOF);
  15.   if (f->buf_max > f->buf_index)
  16.     {
  17.     int result = f->buf[f->buf_index] & 0xFF;
  18.  
  19.     f->buf_index++;
  20. /*
  21. #ifdef DEBUG
  22.     fprintf(stderr, " >%02X", result);
  23. #endif
  24. */
  25.     return(result);
  26.     }
  27.  
  28. /* ok, get a new buf */
  29.   f->file_position += f->buf_max;    /* base pos for next buf */
  30.   f->buf_max = read(f->handle, &f->buf, BUFSIZE);
  31.   f->buf_index = 0;
  32.   if (f->buf_max <= f->buf_index)    /* didn't get anything? */
  33.     {
  34.     f->eof_p = 1;            /* remember the eof */
  35.     if (f->buf_max < 0)
  36.         f->last_file_error = f->buf_max;
  37.         else
  38.         f->last_file_error = 0;
  39. #ifdef DEBUG
  40.     fprintf(stderr, "fgetc(%X)->EOF\r\n", f);
  41. #endif
  42.     return(EOF);
  43.     }
  44.     else
  45.     {
  46.     int result = f->buf[f->buf_index] & 0xFF;
  47.  
  48.     f->buf_index++;
  49. #ifdef DEBUG
  50.     fprintf(stderr, "fgetc(%X)->%02X\r\n", f, result);
  51. #endif
  52.     return(result);
  53.     }
  54. }
  55.